package com.bicon.botu.mall.component.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.component.dto.AppVersionDTO;
import com.bicon.botu.mall.component.common.AppVersionConstants;
import com.bicon.botu.mall.component.entity.AppVersion;
import com.bicon.botu.mall.component.mapper.AppVersionMapper;
import com.bicon.botu.mall.component.service.IAppVersionService;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * app版本Service实现层
 *
 * @Author: 吕祥
 * @Date: Created in 15:41 2017/10/17/017
 */
@Service
public class AppVersionImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements IAppVersionService {

    /**
     * 新增版本
     *
     * @param forcedUpdatingState 强制更新状态(0:非强制，1:强制)
     * @param type                类型：iOS,Android
     * @param versionName         版本名称
     * @param versionCode         版本号
     * @param url                 下载地址
     * @param info                版本说明
     * @param size                文件大小
     * @param times               次数
     * @param state               状态:0未发布，1已发布
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppVersion(String type, String versionName, Integer versionCode, String url,
                                  String info, Integer size, Integer times, String state, String forcedUpdatingState) {
        AppVersion appVersion = new AppVersion();
        appVersion.preInsert();
        appVersion.setType(type);
        appVersion.setVersionName(versionName);
        appVersion.setVersionCode(versionCode);
        appVersion.setUrl(url);
        appVersion.setInfo(info);
        appVersion.setSize(size);
        appVersion.setTimes(times);
        appVersion.setState(state);
        appVersion.setForcedUpdatingState(forcedUpdatingState);
        return appVersion.insert();
    }

    /**
     * 修改版本
     *
     * @param forcedUpdatingState 强制更新状态(0:非强制，1:强制)
     * @param id                  主键id
     * @param type                类型：iOS,Android
     * @param versionName         版本名称
     * @param versionCode         版本号
     * @param url                 下载地址
     * @param info                版本说明
     * @param size                文件大小
     * @param times               次数
     * @param state               状态:0未发布，1已发布
     * @return
     */
    @Override
    public boolean updateAppVersion(String id, String type, String versionName, Integer versionCode, String url,
                                    String info, Integer size, Integer times, String state, String forcedUpdatingState) {
        AppVersion appVersion = new AppVersion().selectById(id);
        if (null == appVersion) {
            return false;
        }
        if (null != type) {
            appVersion.setType(type);
        }
        if (null != versionName) {
            appVersion.setVersionName(versionName);
        }
        if (null != versionCode) {
            appVersion.setVersionCode(versionCode);
        }
        if (null != url) {
            appVersion.setUrl(url);
        }
        if (null != info) {
            appVersion.setInfo(info);
        }
        if (null != size) {
            appVersion.setSize(size);
        }
        if (null != times) {
            appVersion.setTimes(times);
        }
        if (null != state) {
            appVersion.setState(state);
        }
        if (null != forcedUpdatingState) {
            appVersion.setForcedUpdatingState(forcedUpdatingState);
        }
        return appVersion.updateById();
    }

    /**
     * 删除app版本
     *
     * @param id 主键id
     * @return
     */
    @Override
    public boolean deleteAppVersion(String id) {
        AppVersion appVersion = new AppVersion().selectById(id);
        if (null == appVersion) {
            return false;
        }
        return appVersion.deleteById();
    }

    /**
     * 根据id查询版本详情
     *
     * @param id 主键id
     * @return
     */
    @Override
    public AppVersionDTO findOneAppVersion(String id) {
        AppVersion appVersion = new AppVersion().selectById(id);
        if (null == appVersion) {
            return null;
        }
        AppVersionDTO appVersionDTO = new AppVersionDTO();
        BeanMapper.copy(appVersion, appVersionDTO);
        return appVersionDTO;
    }

    /**
     * 分页查询app版本
     *
     * @param pageNo   页数
     * @param pageSize 每页记录数
     * @return
     */
    @Override
    public Page<AppVersion> appVersionPage(Integer pageNo, Integer pageSize) {
        //分页查询，倒序
        Page<AppVersion> page = new AppVersion().selectPage(new Page<AppVersion>(pageNo, pageSize),
                new EntityWrapper<AppVersion>()
                        .eq("logic_delete", AppVersionConstants.DEL_LOGIC_NORMAL)
                        .orderBy("version_code", false));
        return page;
    }

    /**
     * 检查新版本
     *
     * @param versionCode 版本号
     * @return
     */
    @Override
    public BaseResult checkingver(Integer versionCode) {
        AppVersion appVersion = new AppVersion().selectOne(new EntityWrapper<AppVersion>()
                .eq("state", AppVersionConstants.RELEASE)
                .orderBy("version_code", false));
        AppVersionDTO appVersionDTO = new AppVersionDTO();
        appVersionDTO.setUpdate(AppVersionConstants.THERE_IS_NO_NEW_VERSION);
        if (null != appVersion) {
            if (appVersion.getVersionCode() > versionCode) {
                appVersionDTO.setUpdate(AppVersionConstants.THERE_IS_A_NEW_VERSION);
                BeanMapper.copy(appVersion, appVersionDTO);
            }else{
                appVersionDTO.setHotUpdate(appVersion.getHotUpdate());
            }
        }
        return new BaseResult(AppVersionConstants.RESPONSE_CODE_200, AppVersionConstants.RESPONSE_MESSAGE_200, appVersionDTO);
    }
}
